package com.tian.mybatis.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.tian.mybatis.io.Resources;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

/**
 * @author tianwc  公众号：java后端技术全栈、面试专栏
 * @version 1.0.0
 * @date 2023年09月21日 15:06
 * 在线刷题 1200+题和1000+篇干货文章：<a href="https://woaijava.cc/">博客地址</a>
 * <p>
 * 解析mybatis-config.xml配置文件
 *
 * <configuration>
 * <dataSource>
 * <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
 * <property name="url"
 * value="jdbc:mysql://localhost:3306/charge-station?characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=GMT%2B8&amp;allowPublicKeyRetrieval=true"/>
 * <property name="username" value="root"/>
 * <property name="password" value="123456"/>
 * </dataSource>
 * <mappers>
 * <mapper resource="mapper/UserMapper.xml"/>
 * </mappers>
 * </configuration>
 */
public class XmlConfigBuilder {
    private Configuration configuration;

    public XmlConfigBuilder(Configuration configuration) {
        this.configuration = configuration;
    }

    /**
     * 解析mybatis-config.xml配置文件
     *
     * @param inputStream 字节流
     * @return
     */
    public Configuration parse(InputStream inputStream) throws Exception {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(inputStream);
        Element rootElement = document.getRootElement();
        /**
         * <dataSource>
         *         <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
         *         <property name="url"
         *                   value="jdbc:mysql://localhost:3306/charge-station?characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=GMT%2B8&amp;allowPublicKeyRetrieval=true"/>
         *         <property name="username" value="root"/>
         *         <property name="password" value="123456"/>
         * </dataSource>
         */
        List<Element> propertyList = rootElement.selectNodes("//property");
        Properties properties = new Properties();
        for (Element element : propertyList) {
            String name = element.attributeValue("name");
            String value = element.attributeValue("value");
            properties.setProperty(name, value);
        }

        /**
         * 这里就是使用一个连接池，也可以用druid
         */
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        comboPooledDataSource.setDriverClass(properties.getProperty("driver"));
        comboPooledDataSource.setJdbcUrl(properties.getProperty("url"));
        comboPooledDataSource.setUser(properties.getProperty("username"));
        comboPooledDataSource.setPassword(properties.getProperty("password"));
        this.configuration.setDataSource(comboPooledDataSource);

        /**
         * <mappers>
         *    <mapper resource="mapper/UserMapper.xml"/>
         *   </mappers>
         */

        List<Element> mapperList = rootElement.selectNodes("//mapper");
        for (Element element : mapperList) {
            String resource = element.attributeValue("resource");
            InputStream resourceInputStream = Resources.getResourceAsStream(resource);
            /**
             * 解析 所有UserMapper.xml 配置文件
             */
            XmlMapperBuilder xmlMapperBuilder = new XmlMapperBuilder(this.configuration);
            xmlMapperBuilder.parse(resourceInputStream);
        }
        return this.configuration;
    }
}
